import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import StorageUtils from '../../../app/storage';
import update from 'react-addons-update';
import DB from '../../../app/db';


export default Reflux.createStore({
    listenables: [Actions],
    data: {
        ...PageConst,
      userInfo: {
        certNum: '',
        telNum: '',
        vcode: '',
        name:'',
        organName:''
      },
      qrInfo:{},
    },

  initMode: function () {

  },

  initUser: function () {

    let t = this;
    t.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});

    DB.WechatAPI.getUserForWechat({openId: t.data.userInfo.openId}).then(
      function (cb) {
        StorageUtils.setWecharUserInfo(cb);
        t.data.userInfo = update(t.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });

    /*    DB.WechatAPI.getUserForWechat({openId: "onRNb0jjk8ila_XXI9-GKQs483rc"}).then(
          function (content) {
            console.log('home111USER');
            console.info(content);
            this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
          },
          function (error) {
            console.log(error);

          });*/
    t.updateComponent();
  },

    onFetch: function (params, cb) {
        let t = this;
        t.updateComponent();
        cb && cb(t.data);
    },

    updateComponent: function () {
        this.trigger(this.data);
    },

    getInitialState: function () {
        return this.data;
    }
});
